package com.szqhyxj.youxiangju.biz.merchantHome.home;

import android.util.Log;
import android.view.View;

import com.szqhyxj.youxiangju.BaseConsumer;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.core.TokenManager;
import com.szqhyxj.youxiangju.biz.core.UserInfoManger;
import com.szqhyxj.youxiangju.biz.merchantHome.deviceinfo.DeviceInfo;
import com.szqhyxj.youxiangju.sys.BaseResult;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2018/10/30.
 */

public class HomePresenter implements HomeContract.Presenter {

    private HomeContract.View view;

    private List<Disposable> disposables = new ArrayList<>();

    List<Integer> bannerList1 = new ArrayList<>();
    List<Integer> bannerList2 = new ArrayList<>();

    HomePresenter(HomeContract.View view) {
        this.view = view;
    }

    @Override
    public void onDestroy() {
        for (Disposable disposable : disposables) {
            if (disposable != null) {
                disposable.dispose();
            }
        }
    }

    private boolean preCheck() {
        if (!TokenManager.INSTANCE.online()) {
            view.navToLogin();
            return true;
        }
        if (!UserInfoManger.INSTANCE.isRealName()) {
            view.guideToRealName();
            return true;
        }
        return false;
    }

    @Override
    public void init() {
        view.showBanner(getBannerList1(), getBannerList2());
        /*if (UserInfoManger.INSTANCE.getUser().getActiviteStatus() == 0) {

            view.enshowDeviceInfo();
        } else {

         view.showDeviceInfo();
        }*/
    }

    @Override
    public void cashout() {

    }

    @Override
    public void repay() {
        if (preCheck()) {
            return;
        }
        view.navToRepay();
    }

    @Override
    public void receipt() {
        if (preCheck()) {
            return;
        }
        view.navToCashout();
    }

    @Override
    public void vipachievement() {
        if (preCheck()) {
            return;
        }
        if (UserInfoManger.INSTANCE.getUser().getOnlineVip()==0){
            view.navToBeVipachievement();
        }else{
            view.navToVipachievement();
        }

    }

    @Override
    public void partnerperformance() {
        if (preCheck()) {
            return;
        }
        if (UserInfoManger.INSTANCE.getUser().getUserType() == 5 || UserInfoManger.INSTANCE.getUser().getUserType() == 4) {
            view.navToPartnerperformance();
        } else {
            view.goshop();
        }

    }

    @Override
    public void creditCardReview() {
        if (preCheck()) {
            return;
        }
        view.navToCreditCardReview();
    }

    @Override
    public void applyCreditCard() {
        if (preCheck()) {
            return;
        }
        view.navToApplyCreditCard();
    }

    @Override
    public void loadBanner() {
        //目前本地加载图片数据
        //view.showBanner(getBannerList1(),getBannerList2());
        disposables.add(HomeModel.getBanner()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<HomeModel.BannerResult>>() {
                    @Override
                    public void accept(BaseResult<HomeModel.BannerResult> bannerResultBaseResult) throws Exception {
                        if (bannerResultBaseResult.businessLogicSuccess()) {
                            Log.e("图片个数", bannerResultBaseResult.getData().getAdBanner().size() + "------------" + bannerResultBaseResult.getData().getBankBanner().size());
                            List<String> list1 = new ArrayList<>();
                            List<String> list2 = new ArrayList<>();
                            for (int i = 0; i < bannerResultBaseResult.getData().getAdBanner().size(); i++) {
                                list1.add(bannerResultBaseResult.getData().getAdBanner().get(i).getImgUrl());
                            }
                            for (int i = 0; i < bannerResultBaseResult.getData().getBankBanner().size(); i++) {
                                list2.add(bannerResultBaseResult.getData().getBankBanner().get(i).getImgUrl());
                            }
                            Log.e("图片个数", list1.size() + "------------" + list2.size());
                            view.showBanner2(list1, getBannerList2());
                        } else {

                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                }));
                       /* new Consumer<BaseResult<HomeModel.BannerResult>>() {
                            @Override
                            public void accept(BaseResult<HomeModel.BannerResult> banners) throws Exception {
                                if (banners.businessLogicSuccess()){
                                    view.showBanner();
                                }else {
                               //     view.showLoadBannerFail(banners.getMessage());

                                }

                               *//**//* if (banners.businessLogicSuccess()) {
                                    view.showBanner(banners.getData().getAdBanner());
                                    view.showCreditCardBanner(banners.getData().getBankBanner());
                                } else {
                                    view.showLoadBannerFail(banners.getMessage());
                                }*//**//*
                            }
                        },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                             //   view.showLoadBannerFail(throwable.getMessage());
                                view.showBanner();
                            }
                        }
                )
        );*/
    }
    @Override
    public void deviceinit() {
        if (!TokenManager.INSTANCE.online()) {
            view.enshowDeviceInfo();
        } else {

            if (UserInfoManger.INSTANCE.getUser().getUseraccount() == null || UserInfoManger.INSTANCE.getUser().getUseraccount().equals("")) {

            } else {
                MobclickAgent.onProfileSignIn(UserInfoManger.INSTANCE.getUser().getUseraccount() + "");
            }
            if (UserInfoManger.INSTANCE.getUser().getActiviteStatus() == 0) {

                view.enshowDeviceInfo();
            } else {
                if (DeviceInfo.Holder.getInstance().getOnlineState() == null || DeviceInfo.Holder.getInstance().getOnlineState().equals("")) {
                    view.enshowDeviceInfo();
                } else {
                    view.showDeviceInfo();
                }
                view.showDeviceInfo();

            }

        }
    }

    @Override
    public void share() {
        if (preCheck()) {
            return;
        }
        view.gotoshare();
    }

    @Override
    public void getVipInfo() {
        HomeModel.getVipInfo()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<HomeModel.Vipentity>() {
                    @Override
                    protected void error(String code, String msg) {
                        view.showToast(msg);
                    }

                    @Override
                    protected void successAccept(HomeModel.Vipentity vipentity) {
                     if (vipentity==null){
                         view.enshowVipArea();
                     }else{
                         view.showVipArea(vipentity);
                     }
                    }
                });
    }


    private List<Integer> getBannerList1() {
        bannerList1.add(R.mipmap.banner_17);
        bannerList1.add(R.mipmap.banner10);
        bannerList1.add(R.mipmap.banner11);
        return bannerList1;
    }

    private List<Integer> getBannerList2() {
        bannerList2.add(R.mipmap.banner_04);
        bannerList2.add(R.mipmap.banner_05);
        return bannerList2;
    }
}